<?php
class SpaceController extends Zend_Controller_Action
{	
    
 
	function init() //__construct 代替初始化函数
    {
        $this->registry = Zend_Registry::getInstance();
        $this->view = $this->registry['view'];
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }
    
	/*
	 * 显示设置头像页面
	 */
	function indexAction()
    {
		echo $this->view->render('space.phtml');//显示增加模版
    }
    
	
	/*
	 * 保存头像
	 */
	function savephotoAction()
    {
		if ($_FILES['photo']['name'] != '') {
		    $input_array = array();
		    $input_array=$this->_request->getPost();
			if ($input_array['x'] == '' || $input_array['y'] == '' || $input_array['w'] == '' || $input_array['h'] == '') {
				$err_msg = '请剪裁图片';
				echo $err_msg;	
				exit;			
			}
			
			$uploadfile = new Common_Plugin_UploadFile();
			
			$uploadfile->error_mode = 1; //出错处理直接输出错误
			$uploadfile->allow_type = array('jpg','gif','png');
			$uploadfile->max_size = 2048; //图片大小不能大于2M
			$upload = $uploadfile->upfile("photo");	
			if ($uploadfile->errormsg != '') {
				echo $uploadfile->errormsg;
				exit;				
			}
			
			$img_ex = explode(".",$upload['getfilename']);
			/**
			 * 按照原图缩放比例计算截图坐标值缩放百分比
			 */
			$image_info = @getimagesize($upload['filename']);
			$width_percent = $image_info[0]/300;
			$height_percent = $image_info[1]/230;
			/**
			 * 目标图片大w=h=120、50、30
			 */
			$create_size = array('120','50','30');
			/**
			 * 原图片
			 */
			if( $img_ex[1] == "jpg" || $img_ex[1] == "jpeg" ) {
				$img_r = @imagecreatefromjpeg($upload['filename']); 
			}
			if( $img_ex[1] =="gif" ) {
				$img_r = @imagecreatefromgif($upload['filename']); 
			}
			if( $img_ex[1] == "png" ) {
				$img_r = @imagecreatefrompng($upload['filename']); 
			}

			/**
			 * 开始生成
			 */
			$targ_w = $targ_h = $dst_r = $filename_small = $show_photo = '';
			foreach ($create_size as $size) {
				$targ_w = $targ_h = $size;
				$filename_small =  $img_ex[0] . '_'. $size .'.' . $img_ex[1];
				/**
				 * 上传成功大图作为前台回显
				 */
				if ($size == '120') {
					$show_photo = $filename_small;
				}
				/**
				 * 目标图片
				 */
				$dst_r = @ImageCreateTrueColor( $targ_w, $targ_h );
				@imagecopyresampled($dst_r,$img_r,0,0,$input_array['x']*$width_percent,$input_array['y']*$height_percent,$targ_w,$targ_h,$input_array['w']*$width_percent,$input_array['h']*$height_percent);
				
				if( $img_ex[1] == "jpg" || $img_ex[1] == "jpeg" ) {
					@imagejpeg($dst_r,$filename_small);
				}
				if( $img_ex[1] =="gif" ) {
					@imagegif($dst_r,$filename_small);
				}
				if( $img_ex[1] == "png" ) {
					@imagepng($dst_r,$filename_small);
				}					
			}
			
			
		} else {
			$err_msg = '上传的图片不能为空';
			echo $err_msg;
			exit;			
		}
    }
	
	/*
	 * 显示设置头像页面
	 */
	function showAction()
    {
		echo $this->view->render('space_show.phtml');//显示增加模版
    }
    
    
    /*
	 * 删除文件
	 */
	function unlinkAction()
    {
		$temp_photo=$this->_request->getPost('temp_photo');
		file_exists($temp_photo) && unlink($temp_photo);
    }

    /*
	 * 保存裁剪头像
	 */
	function saveavatarAction()
    {
		    $input_array = array();
		    $input_array=$this->_request->getPost();
			if ($input_array['x'] == '' || $input_array['y'] == '' || $input_array['w'] == '' || $input_array['h'] == '') {
				$err_msg = '请剪裁图片';
				echo $err_msg;	
				exit;			
			}
			
			if ($input_array['avatar'] == ''){
				$err_msg = '上传的原图片不存在';
				echo $err_msg;	
				exit;			
			}
			
			
			$img_ex = explode(".",$input_array['avatar']);
			/**
			 * 按照原图缩放比例计算截图坐标值缩放百分比
			 */
			$image_info = @getimagesize($input_array['avatar']);
			$width_percent = $image_info[0]/300;
			$height_percent = $image_info[1]/230;
			/**
			 * 目标图片大w=h=120、50、30
			 */
			$create_size = array('120','50','30');
			/**
			 * 原图片
			 */
			if( $img_ex[1] == "jpg" || $img_ex[1] == "jpeg" ) {
				$img_r = @imagecreatefromjpeg($input_array['avatar']); 
			}
			if( $img_ex[1] =="gif" ) {
				$img_r = @imagecreatefromgif($input_array['avatar']); 
			}
			if( $img_ex[1] == "png" ) {
				$img_r = @imagecreatefrompng($input_array['avatar']); 
			}

			/**
			 * 开始生成
			 */
			$targ_w = $targ_h = $dst_r = $filename_small = $show_photo = '';
			foreach ($create_size as $size) {
				$targ_w = $targ_h = $size;
				$filename_small =  $img_ex[0] . '_'. $size .'.' . $img_ex[1];
				/**
				 * 上传成功大图作为前台回显
				 */
				if ($size == '120') {
					$show_photo = $filename_small;
				}
				/**
				 * 目标图片
				 */
				$dst_r = @ImageCreateTrueColor( $targ_w, $targ_h );
				@imagecopyresampled($dst_r,$img_r,0,0,$input_array['x']*$width_percent,$input_array['y']*$height_percent,$targ_w,$targ_h,$input_array['w']*$width_percent,$input_array['h']*$height_percent);
				
				if( $img_ex[1] == "jpg" || $img_ex[1] == "jpeg" ) {
					@imagejpeg($dst_r,$filename_small);
				}
				if( $img_ex[1] =="gif" ) {
					@imagegif($dst_r,$filename_small);
				}
				if( $img_ex[1] == "png" ) {
					@imagepng($dst_r,$filename_small);
				}					
			}
			//删除原图
			file_exists($input_array['avatar']) && unlink($input_array['avatar']);
		
    }


	
}
	
	
	